package com.zp.business.impl.register;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zp.base.api.RemoteClinicForRegistService;
import com.zp.base.api.RemoteSerialNumberUtils;
import com.zp.business.entity.patient.ClinicMaster;
import com.zp.business.entity.patient.PatMasterIndex;
import com.zp.business.entity.patient.bo.ClinicMasterBo;
import com.zp.business.entity.patient.bo.PatMasterIndexBo;
import com.zp.business.entity.register.RegisterPayType;
import com.zp.business.entity.register.RegistrationItem;
import com.zp.business.entity.register.bo.RegisterFormBo;
import com.zp.business.entity.register.bo.RegisterPayTypeBo;
import com.zp.business.entity.register.bo.RegistrationItemBo;
import com.zp.business.service.patient.IClinicMasterService;
import com.zp.business.service.patient.IPatMasterIndexService;
import com.zp.business.service.register.IClinicMasterCostService;
import com.zp.business.service.register.ICurrentRegisterService;
import com.zp.business.service.register.IRegistPayTypeService;
import com.zp.business.service.register.IRegistrationItemService;
import com.zp.common.core.utils.DateUtils;
import com.zp.common.core.utils.StringUtils;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: zp-backstage
 * @ClassName CurrentRegisterServiceImpl
 * @description:
 * @author: zhangpeng
 * @create: 2023-12-26 16:43
 * @Version 1.0
 **/
@RequiredArgsConstructor
@Service
public class CurrentRegisterServiceImpl implements ICurrentRegisterService {

    private final IClinicMasterService clinicMasterService;
    @DubboReference
    private RemoteSerialNumberUtils remoteSerialNumberUtils;
    private final IRegistPayTypeService registPayTypeService;
    private final IClinicMasterCostService clinicMasterCostService;
    private final IRegistrationItemService registrationItemService;
    private final IPatMasterIndexService patMasterIndexService;
    //    @DubboReference
//    private RemoteClinicMasterService clinicMasterService;
//    @DubboReference
//    private RemotePatMasterIndexService remotePatMasterIndexService;
    @DubboReference
    private RemoteClinicForRegistService clinicForRegistService;

    /**
     * 挂号保存
     *
     * @param bo
     * @return
     */
    @Override
    @GlobalTransactional
    public boolean add (RegisterFormBo bo) {
        // 获取患者信息
        ClinicMasterBo clinicMasterBo = bo.getPatientInfo();
        // 获取支付类型
        List<RegisterPayTypeBo> registerPayTypeBoList = bo.getPayWayInfoList();
        // 获取费用明细
        List<RegistrationItemBo> registrationItemList = bo.getRegistrationItemBoList();
        // 更新当前号
        clinicForRegistService.updateCurrentNum(clinicMasterBo.getClinicForRegistId(), bo.getOrgId());
        PatMasterIndexBo patMasterIndexBo = new PatMasterIndexBo();
        // 组装患者住信息表数据
        BeanUtil.copyProperties(bo.getPatientInfo(), patMasterIndexBo);
        PatMasterIndex patMasterIndex = patMasterIndexService.insertByBoRetPMI(patMasterIndexBo);
        if (ObjectUtil.isNotNull(patMasterIndex)) {
            // 获取患者信息主记录id
            String patientId = patMasterIndex.getId();
            clinicMasterBo.setPatientId(patientId);
            // 判断是否为当日挂号，真 ：设置就诊日期为当前
            if (StringUtils.equals(clinicMasterBo.getRegistType(), "0")) {
                clinicMasterBo.setVisitDate(DateUtils.dateTime("yyyy-MM-dd", DateUtils.getDate()));
            }
            // 获取门诊号
            String clinicNo = remoteSerialNumberUtils.generateClinicNo(bo.getOrgId());
            // 设置门诊号
            clinicMasterBo.setClinicNo(clinicNo);
            // 设置挂号状态（1、未就诊、2、已就诊）
            clinicMasterBo.setClinicType("1");
            clinicMasterBo.setDelFlag("0");
            clinicMasterBo.setVisitNo(Integer.valueOf(clinicNo.substring(8)));
            // 新增挂号记录
            ClinicMaster clinicMaster = clinicMasterService.insertByBoRetCM(clinicMasterBo);
            // 获取患者挂号信息id = clinicId
            String clinicId = clinicMaster.getId();
            // 保存支付方式
            List<RegisterPayType> itemList = new ArrayList<>();
            registerPayTypeBoList.forEach(item -> {
                item.setClinicId(clinicId);
                item.setDelFlag("0");
                itemList.add(BeanUtil.toBean(item, RegisterPayType.class));
            });
            registPayTypeService.insertBatch(itemList);
            // 保存挂号明细
            List<RegistrationItem> registPayTypeList = new ArrayList<>();
            registrationItemList.forEach(item -> {
                item.setClinicId(clinicId);
                item.setDelFlag("0");
                item.setOrgId(bo.getOrgId());
                item.setNum(BigDecimal.valueOf(1));
                registPayTypeList.add(BeanUtil.toBean(item, RegistrationItem.class));
            });
            // 保存挂号明细
            registrationItemService.insetBatch(registPayTypeList);
        } else {
            throw new RuntimeException("挂号失败，请联系管理员");
        }
        return true;
    }
}
